package ru.ipccenter.webBeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import ru.ipccenter.dao.DataSource;
import ru.ipccenter.dao.FileResult;
import ru.ipccenter.dao.databases.OracleDatabase;
import ru.ipccenter.resource.UserQuery;

/**
 *
 * @author fedich
 */
@ManagedBean(name = "MainSearch")
@SessionScoped
public class MainSearchBean implements Serializable {

    private String fileName;
    private String type;    //selected type
    private List<FileResult> resultSearch;
    private List<String> types;
    private List<DataSource> dataSources;       // all avalible data  

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<String> getTypes() {
        return types;
    }

    public void setTypes(ArrayList<String> types) {
        this.types = types;
    }
    //getters and setters above

    public void doAction() {
        try {
            OracleDatabase oracleSource = OracleDatabase.getInstance();
            
            if (UserQuery.isRegExp(fileName)) {
                resultSearch = oracleSource.resultMainRegExp(fileName);
            } else {
                resultSearch = oracleSource.resultMainKeywords(fileName);
            }            
        } catch (Exception ex) {
            Logger.getLogger(MainSearchBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<FileResult> getResultSearch() {
        return resultSearch;
    }

    public MainSearchBean() {
        types = new ArrayList<String>();
        types.add("Music");
        types.add("Video");
        types.add("Folders");
        types.add("Files");
        types.add("All");

        resultSearch = new ArrayList<FileResult>();
    }
}
